<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use app\lib\redis\Predis;

class Login extends Controller
{

    //登录
    public function index()
    {
    	
        $mobile = $_POST['mobile'];
        $code = $_POST['code'];

        if(empty($mobile) || empty($code)){
            return json_encode(['errorCode'=>1000,'msg'=>'paramter error']);
        }

        try{
            $redisCode = Predis::instance()->get(Predis::smsKey($mobile));
        }catch(\Exception $e){
            echo $e->getMessage();
        }

        if($redisCode == $code){
            $data = [
                'user'=>$mobile,
                'srcKey'=>md5(Predis::userKey($mobile)),
                'time'=>time(),
                'isLogin'=>true
            ];
            Predis::instance()->set(Predis::userKey($mobile),$data);
            return json_encode(['errorCode'=>0,'msg'=>'login success','data'=>$data]);
        }else{
            return json_encode(['errorCode'=>1000,'msg'=>'login error']);
        }
        
    }
}
